package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.CreateReport_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class CreateReportController extends Controller {

    private final FormFactory formFactory;
    private final CreateReport_Repository createReport_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public CreateReportController(FormFactory formFactory, CreateReport_Repository createReport_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.createReport_Repository = createReport_Repository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> ReportByUserMonth(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String StartDate = json.findPath("StartDate").textValue();
		
	    return createReport_Repository.ReportByUserMonth(PhoneNumber, StartDate).thenApplyAsync(_GetListByProc -> {
	    	return ok(Json.toJson(_GetListByProc));
	    }, ec.current());
    }
    
    public CompletionStage<Result> ReportByProduct(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String HWMC = json.findPath("HWMC").textValue();
		
	    return createReport_Repository.ReportByProduct(PhoneNumber, HWMC).thenApplyAsync(_GetListByProc -> {
	    	return ok(Json.toJson(_GetListByProc));
	    }, ec.current());
    }
    
    public CompletionStage<Result> ReportByMonthAll(){
		JsonNode json = request().body().asJson();
		String StartDate = json.findPath("StartDate").textValue();
		
	    return createReport_Repository.ReportByMonthAll(StartDate).thenApplyAsync(_GetListByProc -> {
	    	return ok(Json.toJson(_GetListByProc));
	    }, ec.current());
    }

	public CompletionStage<Result> CreateReport(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String StartDate = json.findPath("StartDate").textValue();
		String EndDate = json.findPath("EndDate").textValue();
		String HWMC = json.findPath("HWMC").textValue();
		String KHMC = json.findPath("KHMC").textValue();
		String MonthFlag = json.findPath("MonthFlag").textValue();

		return createReport_Repository.CreateReport(PhoneNumber, StartDate, EndDate, HWMC, KHMC, MonthFlag).thenApplyAsync(_GetListByProc -> {
			return ok(Json.toJson(_GetListByProc));
		}, ec.current());
	}

	public CompletionStage<Result> StaticReport(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String StartDate = json.findPath("StartDate").textValue();
		String EndDate = json.findPath("EndDate").textValue();
		String HWMC = json.findPath("HWMC").textValue();
		String KHMC = json.findPath("KHMC").textValue();
		String MonthFlag = json.findPath("MonthFlag").textValue();

		return createReport_Repository.StaticReport(PhoneNumber, StartDate, EndDate, HWMC, KHMC, MonthFlag).thenApplyAsync(_GetListByProc -> {
			return ok(Json.toJson(_GetListByProc));
		}, ec.current());
	}

	public CompletionStage<Result> getDetailList(){
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String StartDate = json.findPath("StartDate").textValue();
		String EndDate = json.findPath("EndDate").textValue();
		String ProductType = json.findPath("ProductType").textValue();
		String ProductName = json.findPath("ProductName").textValue();
		String CompanyName = json.findPath("CompanyName").textValue();
		String unloadPlace = json.findPath("unloadPlace").textValue();
		String mileageDef = json.findPath("mileageDef").textValue();
		String plateNo = json.findPath("plateNo").textValue();

		return createReport_Repository.getDetailList(PhoneNumber, StartDate, EndDate, ProductType, ProductName, CompanyName, unloadPlace, mileageDef, plateNo).thenApplyAsync(_result -> {
			return ok(Json.toJson(_result));
		}, ec.current());
	}

}